<template>
  <div class="login_container">
    <div class="main">
      <div id="head">
        <span>忘记密码</span>
        <el-popover
          placement="top-start"
          class="popover"
          width="200"
          trigger="hover"
        >
        </el-popover>
      </div>
      <el-form
        :model="form"
        status-icon
        :rules="rules"
        ref="passRuleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 账户登录 -->
        <div class="infos">
          <el-form-item label="" prop="email">
            <el-input
              class="inputbox"
              placeholder="请输入账户绑定的邮箱"
              prefix-icon="el-icon-message"
              type="text"
              v-model="form.email"
            ></el-input>
          </el-form-item>
        </div>
        <div class="bottom">
          <el-button
            size="normal"
            plain
            type="primary"
            @click="sendEmail('passRuleForm')"
            >发送重置邮件</el-button
          >
        </div>
      </el-form>
      <div class="getpassword">
        <span @click="toLogin" class="text">返回登录 ...</span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    var validateEmail = async (rule, value, callback) => {
      // console.log("valulll", value);
      // var reg =
      //   /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/; //正则表达式
      // if (value == "" || value == null) {
      //   callback(new Error("邮箱不能为空"));
      // } else 
      // if (!reg.test(value)) {
      //   callback(new Error("请输入合法的邮箱地址！"));
      // } else {
        const res = await this.$axios.get(
          `${this.HOST}/user/api/auth/validateEmail/` + this.form.email
        );
        if (res.data.data.status == 500) {
          callback(new Error("邮箱不存在！"));
        } else {
          callback();
        }
      // }
    };
    return {
      form: {
        email: "",
      },
      rules: {
        email: [{ validator: validateEmail, trigger: "blur" }],
      },
    };
  },
  created() {},
  methods: {
    /**
     * 检查邮是否存在
     */
    sendEmail(formName) {
      let _this = this;
      this.$refs[formName].validate(async (valid) => {
        console.log("valid", valid);
        if (valid) {
          const res = await this.$axios.get(
            `${this.HOST}/user/api/auth/applyRestPassword/` + _this.form.email
          );
          if (res.data.meta.code == 200) {
            // console.log("pass", res);
            this.$message.success("邮件发送成功！");
            this.form.email = "";
            this.toLogin();
          }
        } else {
          return false;
        }
      });
    },
    /**
     * 点击忘记密码
     */
    async toLogin() {
      this.$emit("to-login");
    },
  },
};
</script>
<style lang='less' scope>
.login_container {
  // border: 1px black solid ;
  // height: 100%;
  // width: 100%;
  top: 35%;

  .main {
    width: 450px;
    // display: inline-block;
    padding-bottom: 10px;
    margin: auto;
    background-color: rgba(255, 255, 255, 0.4) !important;
    // background-color: rgba(255, 255, 255, 1) !important;
    // position: absolute;
    // left: 50%;
    // top: 50%;
    box-shadow: 0px 15px 25px rgba(0, 0, 0, 0.5);
    /*边框阴影  水平阴影0 垂直阴影15px 模糊25px 颜色黑色透明度0.5*/
    // border-radius: 10px;

    .infos {
      // padding-right: 80px;
      width: 350px;

      /deep/.inputbox {
        border-radius: 10px;
        background-color: rgba(255, 255, 255, 0.377);

        &::placeholder {
          color: rgb(114, 112, 112);
        }
      }

      /deep/ .el-input__inner {
        background: blue !important;
      }
    }

    #head {
      font-weight: bold;
      text-align: center;
      width: 100%;
      height: 70px;
      line-height: 60px;
      color: rgb(85, 85, 85);
      font-size: 22px;

      .popover {
        z-index: 10;
        margin-left: 5px;
      }
    }

    .bottom {
      width: 200px;
      margin: 20px auto;
      // border: 1px solid black;
      display: flex;
      justify-content: center;

      // .forget {
      //   font-size: 14px;
      //   color: #dcddd8;

      //   .word {
      //     cursor: pointer;
      //     margin-right: 40px;
      //     color: #dcddd8;
      //   }
      //   .word:hover {
      //     // position: relative;
      //     // bottom: 0px;
      //     // padding: 0px;
      //     margin-right: 40px;
      //     cursor: pointer;
      //     color: rgb(46, 158, 255);
      //     text-decoration: underline;
      //   }
      // }
    }
  }
}

.getpassword {
  width: 100%;
  text-align: end;
  font-size: 16px;

  .text {
    display: inline-block;
  }

  .text:hover {
    font-weight: bold;
    cursor: pointer;
  }
}
</style>
